package hos.ui.spinner;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

import hos.adapter.CommonArrayAdapter;

/**
 * <p>Title: SpinnerSelected </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2022/4/28 13:24
 */
public class SpinnerExtend extends Spinner {

    public SpinnerExtend(Context context) {
        super(context);
    }

    public SpinnerExtend(Context context, int mode) {
        super(context, mode);
    }

    public SpinnerExtend(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SpinnerExtend(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SpinnerExtend(Context context, AttributeSet attrs, int defStyleAttr, int mode) {
        super(context, attrs, defStyleAttr, mode);
    }

    public void setOnSelectedClickListener(OnItemClickListener listener) {
        if (listener == null) {
            setOnItemSelectedListener(null);
            return;
        }
        setOnItemSelectedListener(new OnItemSelectedListener() {
            @SuppressWarnings("ConstantConditions")
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (listener == null) {
                    return;
                }
                listener.onItemClick(SpinnerExtend.this, getSelectedItem(), position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    public interface OnItemClickListener {
        void onItemClick(SpinnerExtend spinner, Object item, int position);
    }


    public void onDestroy(){
        SpinnerAdapter adapter = getAdapter();
        if (adapter instanceof CommonArrayAdapter) {
            ((CommonArrayAdapter<?>) adapter).onDestroy();
        }
        setOnSelectedClickListener(null);
    }
}
